package ru.fadeev.crammer.ui.window;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


/**
 * AddCorpusTextDialog provides GUI for adding phrases to PhrasesCorpus by
 * entering some text in text area.
 * @author Konstantin Fadeyev
 *
 */
public class AddCorpusTextDialog extends JDialog {
	// Constants
	public final static int BTN_OK = 100;
	public final static int BTN_CANCEL = 101;
	
	// UI Components
	private JPanel mainPanel;
	private JScrollPane textScrollPane;
	private JTextArea textArea;
	   
	private JPanel bottomPanel;
	private JPanel okCancelButtonsPanel;
    private JButton okButton;
    private JButton cancelButton;
    
	// User fields
	private int pressedButton;
    
	
	/**
	 * Constructs AddCorpusTextDialog
	 * @param owner parent window
	 */
    public AddCorpusTextDialog(Dialog owner) {
        super(owner,true);
        	
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        setSize(500, 500);
        setLocation((screenSize.width-500)/2, (screenSize.height-500)/3);
        
       	setTitle("Customizing corpus of phrases");
       
        initComponents();
        placeComponents();
    }

    
    /**
     * Initializs UI Components of dialog
     */
    private void initComponents() {
        mainPanel = new JPanel();
        bottomPanel = new JPanel();
        okCancelButtonsPanel = new JPanel();

        textArea = new JTextArea();
        JTextField doomy = new JTextField();
        textArea.setFont(doomy.getFont());
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
    	textScrollPane = new JScrollPane(textArea);

        okButton = new JButton("OK");
        getRootPane().setDefaultButton(okButton);
        cancelButton = new JButton("Cancel");
        okButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	okButtonAction();
			        }
			    });
        
        cancelButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			           	cancelButtonAction();
			        }
		        });
        setContentPane(mainPanel);
    }
    
   
    /**
     * Places UI components using different layout managers
     */
    private void placeComponents() {
    	mainPanel.setLayout(new BorderLayout());
    	mainPanel.add(textScrollPane, BorderLayout.CENTER);
    	mainPanel.add(bottomPanel, BorderLayout.SOUTH);
    
    	bottomPanel.setPreferredSize(new Dimension(200, 40));
    	bottomPanel.setLayout(new BorderLayout());
    	bottomPanel.add(okCancelButtonsPanel, BorderLayout.EAST);
    	
    	okCancelButtonsPanel.setPreferredSize(new Dimension(210, 40));
    	okCancelButtonsPanel.setLayout(null);
    	okCancelButtonsPanel.add(okButton); 
    	okCancelButtonsPanel.add(cancelButton);
    	
    	okButton.setBounds(10, 10, 90, 20);
    	cancelButton.setBounds(110, 10, 90, 20);
    }
     
    
    /**
     * Ok button action handler. Validates entered values.
     */
    private void okButtonAction() {
    	if(textArea.getText().trim().equals("")) {
    		JOptionPane.showMessageDialog(this, "Enter some text to continue.",
    				"Warning", JOptionPane.WARNING_MESSAGE);
    		textArea.requestFocus();
    		return;
    	}    	
    	pressedButton = BTN_OK;
    	setVisible(false);
    }
      
    
    /**
     * Cancel button action handler. Closes dialog.
     */
    private void cancelButtonAction() {
        pressedButton = BTN_CANCEL;
        setVisible(false);
    }
      
    
    /**
     * After dialog is closed, call this function to find out which option
     * user picked (OK or Cancel)
     * @return pressed button (one of BTN_OK, BTN_CANCEL)
     */
    public int getPressedButton() {
    	return pressedButton;
    }
    
    
    /**
     * After dialog is closed, call this function to get entered text
     * @return text from text area of dialog
     */
    public String getText() {
    	return textArea.getText();
    }
}
